<?
	include("includes/config.php");
	include("includes/login.php");
	include("includes/functions.php");
	if(@$_GET['adauga_factura']){
		
		$id_comanda = $_GET['id_comanda'];
		$id_utilizator = $_SESSION['id_utilizator'];
		

		
		$res_client = mysql_query("select * from comenzi, parteneri where comenzi.id_client = parteneri.id_client and id_comanda = '$id_comanda'");
		$row_client = mysql_fetch_array($res_client);
		
		$nume_cumparator = $row_client['nume'];
		$cump_nr_ord_rc = $row_client['nr_ord_rc'];
		$cump_cod_fiscal = $row_client['CF'];
		$cump_banca = $row_client['banca'];
		$cump_cont =  $row_client['cont'];
		
		
		
		$res_unitate  = mysql_query("select * from setari_unitate where id_utilizator='$id_utilizator'");
		$row_unitate = mysql_fetch_array($res_unitate);
		
		$serie_factura = $row_unitate['serie_factura'];
		$numar = $row_unitate['numar_factura'] + 1;
		$unitate = $row_unitate['nume_firma'];
		$cod_fiscal = $row_unitate['cod_fiscal'];
		$sediu = $row_unitate['sediu'];
		$nr_ord_rc = $row_unitate['nr_ord_rc'];
		$cont = $row_unitate['cont'];
		$banca = $row_unitate['banca'];
		$pctLcr = $_SESSION['pctLucru'];
		$respopctlcr = mysql_query("select adresa from setari_punct_lucru where id_unitate = '$pctLcr' ");
		$rowpctlcr = mysql_fetch_array($respopctlcr);
		$punct_lucru = $rowpctlcr['adresa'];
		$cod_fiscal = $row_unitate['cod_fiscal'];
		
		
		$res_com = mysql_query("select * from comenzi,parteneri_masini where id_comanda = '$id_comanda' and comenzi.numar_inmatriculare = parteneri_masini.numar_inmatriculare");
		$rowCom = mysql_fetch_array($res_com);
		$numar_inmat = $rowCom['numar_inmatriculare'];
		
		if(@$_GET['suma_totala']){
				$sumatot = $_GET['suma_totala'];
		}else{
			$res_deviz = mysql_query("select suma_totala from comenzi where id_comanda = '$id_comanda' ");
			$row_deviz = mysql_fetch_array($res_deviz);
			$sumatot =  $row_deviz['suma_totala'];
		}
		
	
		
		$chFac = mysql_query("select id_comanda from facturi where id_comanda = '$id_comanda'");
		if(mysql_num_rows($chFac) == 0){
			mysql_query("insert into facturi
											(id,
											serie,
											id_comanda,
											numar,
											data,
											data_livrari,
											unitate,
											nr_ord_rc,
											cod_fiscal,
											sediu,
											punct_lucru,
											cont,
											banca,
											cumparator,
											cump_nr_ord_rc,
											cump_cod_fiscal,
											cump_cont,
											cump_banca,
											nr_inmat,
											status_factura,
											suma_totala
											) 
											VALUES
											(NULL,
											'$serie_factura',
											'$id_comanda',
											'$numar',
											NOW(),
											NOW(),
											'$unitate',
											'$nr_ord_rc',
											'$cod_fiscal',
											'$sediu',
											'$punct_lucru',
											'$cont',
											'$banca',
											'$nume_cumparator',
											'$cump_nr_ord_rc',
											'$cump_cod_fiscal',
											'$cump_cont',
											'$cump_banca',
											'$numar_inmat',
											'deschisa',
											'$sumatot'
											)")or die(mysql_error());
						$nr_fact = $numar;					
			mysql_query("update setari_unitate set numar_factura  = '$nr_fact ' ");
			header("Location: /facturi.php");
			
		}else{
			
		}
	}
	
	
?>
<html>
	<head>
		<link rel="stylesheet" type="text/css" href="css/style.css" />
	</head>
	<body>
		<div id="header"><h1> <? echo detaliFirma('nume_firma'); ?></h1></div>
		<div id="warper">
			<?
				$resMeniu = mysql_query("select * from meniu");
			?>
			<div id="menu">
				<ul>
					<? while($rowMen = mysql_fetch_array($resMeniu)): ?>
					<li><a href="<?echo $rowMen['link'];?>"><? echo $rowMen['numeLink']; ?></a></li>
					<? endwhile; ?>
				</ul>
			</div>
			<div id="content">
				<div id="contentWrap">
					<div id="submeniu_facturi" class="block">
						
						<a class="buton" href="/facturi.php">Facturi Deschise</a>&nbsp; <a href="facturi_incheiate.php" class="buton">Facturi Incheiate</a>
					</div>
					
					<table>
						<tr id="capTabel">
							<td >Sereie/Nr. Factura</td>
							<td>Client</td>
							<td>Nr. Inmatrc.</td>
							<td>Data Facura</td>
							<td>Editeaza Factura</td>
						</tr>
						<?
								$resfact = mysql_query("select * from facturi where status_factura = 'deschisa' ");
								while($rowfact = mysql_fetch_array($resfact)):
						?>
						<tr style="height:110px;">
							<td ><? echo $rowfact['serie']. $rowfact['numar']; ?></td>
							<td><? echo $rowfact['cumparator']; ?></td>
							<td><? echo $rowfact['nr_inmat']; ?></td>
							<td><? echo $rowfact['data']; ?></td>
							<td align="center">
								<a class="buton" target="_blank" href="/documente/factura_simpla.php?id_factura=<?echo $rowfact['id'];?>&exFurnizor=1" onclick="window.open('/documente/factura_simpla.php?id_factura=<?echo $rowfact['id'];?>&exClient=2','newwin');">Listeaza</a> <br/><br/>
								<a class="buton" target="_blank" href="/documente/factura_simpla.php?id_factura=<?echo $rowfact['id'];?>&exFurnizor=1&incasare=true" onclick="window.open('/documente/factura_simpla.php?id_factura=<?echo $rowfact['id'];?>&exClient=2&incasare=true','newwin');">Listeaza cu incasare</a>
								<br/><br/>
								<a class="buton" target="_blank" href="/documente/chitanta.php?id_factura=<?echo $rowfact['id'];?>&exFurnizor=1&incasare=true" onclick="window.open('/documente/chitanta.php?id_factura=<?echo $rowfact['id'];?>&exClient=2&incasare=true','newwin');">Listeaza CHITANTA</a>
								</td>
						</tr>
						
						<? endwhile; ?>
					</table>
					
				</div>
			</div>
		</div>
	</body>
</html>